package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.*;
import com.xjc.business.entity.po.classadmin.ClassActivityPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.classadmin.ClassActivityVo;
import com.xjc.business.entity.vo.stu.ActivityVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.ActivitiesMapper;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author root
 * @description 针对表【activities(宿舍评优活动表)】的数据库操作Service实现
 * @createDate 2025-01-25 10:24:30
 */
@Service
public class ActivitiesServiceImpl extends ServiceImpl<ActivitiesMapper, Activities>
        implements ActivitiesService {

    @Autowired
    @Lazy
    WxUserService wxUserService;

    @Autowired
    @Lazy
    AllStudentRegsiterRelationsService allStudentRegsiterRelationsService;

    @Autowired
    @Lazy
    AllStudentStayRelationsService allStudentStayRelationsService;

    @Autowired
    @Lazy
    ClassAdminRelationsService classAdminRelationsService;

    @Autowired
    @Lazy
    CollageAdminRelationsService collageAdminRelationsService;

    @Autowired
    @Lazy
    BuildingAdminRelationsService buildingAdminRelationsService;

    /**
     * 获取宿舍评优活动列表
     *
     * @param pageSize     每页显示条数
     * @param currentPage  当前页
     * @param classAdminId 辅导员id
     * @param status       状态
     * @return Result
     */
    @Override
    public Result<PageVo<ClassActivityVo>> getActivityPage(Integer pageSize, Integer currentPage, Integer classAdminId, Integer status) {
        LambdaQueryWrapper<Activities> activitiesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        activitiesLambdaQueryWrapper.eq(Activities::getPublishId, classAdminId);
        Date nowDate = new Date();
        if (status == 0) {
            // 过期
            activitiesLambdaQueryWrapper.le(Activities::getEndTime, nowDate);
        } else if (status == 1) {
            // 未过期
            activitiesLambdaQueryWrapper.ge(Activities::getEndTime, nowDate);
        }
        // 全部
        Page<Activities> activitiesPage = new Page<>(currentPage, pageSize);
        page(activitiesPage, activitiesLambdaQueryWrapper);
        List<Activities> activitiesPageRecords = activitiesPage.getRecords();
        List<ClassActivityVo> classActivityVoList = activitiesPageRecords.stream().map(activities -> {
            ClassActivityVo classActivityVo = new ClassActivityVo();
            classActivityVo.setId(activities.getId());
            classActivityVo.setPic(activities.getPic());
            classActivityVo.setTitle(activities.getTitle());
            classActivityVo.setStartTime(activities.getStartTime());
            classActivityVo.setEndTime(activities.getEndTime());
            classActivityVo.setCreateTime(activities.getCreateTime());
            return classActivityVo;
        }).collect(Collectors.toList());
        PageVo<ClassActivityVo> pageVo = new PageVo<>();
        pageVo.setData(classActivityVoList);
        pageVo.setCurrent(currentPage);
        pageVo.setSize(pageSize);
        pageVo.setTotal(activitiesPage.getTotal());
        pageVo.setPageCount(activitiesPage.getPages());
        return Result.okWithData(pageVo);
    }

    /**
     * 添加宿舍评优活动
     *
     * @param classActivityPo 宿舍评优活动
     * @return Result
     */
    @Override
    public Result<String> addActivity(ClassActivityPo classActivityPo) {
        Activities activities = new Activities();
        activities.setPublishId(classActivityPo.getClassAdminId());
        activities.setContent(classActivityPo.getContent());
        activities.setPic(classActivityPo.getPic());
        activities.setTitle(classActivityPo.getTitle());
        activities.setLevel(1);
        activities.setStartTime(classActivityPo.getStartTime());
        activities.setEndTime(classActivityPo.getEndTime());
        activities.setCreateTime(new Date());
        activities.setUpdateTime(new Date());
        save(activities);
        return Result.okWithData("添加成功");
    }

    /**
     * 获取宿舍评优活动列表
     * @param wxId        微信id
     * @param level       级别
     * @return Result
     */
    @Override
    public Result<List<ActivityVo>> getActivityListWithLevel(Integer wxId, Integer level) {

        Integer stuId = wxUserService.getById(wxId).getStuId();

        LambdaQueryWrapper<AllStudentRegsiterRelations> allStudentRegsiterRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        allStudentRegsiterRelationsLambdaQueryWrapper.eq(AllStudentRegsiterRelations::getStuId, stuId);
        AllStudentRegsiterRelations studentFromDB = allStudentRegsiterRelationsService.getOne(allStudentRegsiterRelationsLambdaQueryWrapper);

        LambdaQueryWrapper<Activities> activitiesLambdaQueryWrapper = new LambdaQueryWrapper<>();


        if (level == 0) {
            // 宿舍
            LambdaQueryWrapper<AllStudentStayRelations> allStudentStayRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            allStudentStayRelationsLambdaQueryWrapper.eq(AllStudentStayRelations::getStuId, stuId);
            AllStudentStayRelations allStudentStayRelations = allStudentStayRelationsService.getOne(allStudentStayRelationsLambdaQueryWrapper);
            Integer buildId = allStudentStayRelations.getBuildId();

            LambdaQueryWrapper<BuildingAdminRelations> buildingAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            buildingAdminRelationsLambdaQueryWrapper.eq(BuildingAdminRelations::getBuildId, buildId);
            BuildingAdminRelations buildingAdminRelations = buildingAdminRelationsService.getOne(buildingAdminRelationsLambdaQueryWrapper);
            if(buildingAdminRelations == null){
                return Result.okWithData(new ArrayList<>());
            }
            Integer buildAdminId = buildingAdminRelations.getBuildAdminId();
            activitiesLambdaQueryWrapper.eq(Activities::getPublishId, buildAdminId);

        } else if (level == 1) {
            // 班级
            Integer clazzId = studentFromDB.getClazzId();
            LambdaQueryWrapper<ClassAdminRelations> classAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            classAdminRelationsLambdaQueryWrapper.eq(ClassAdminRelations::getClId, clazzId);
            ClassAdminRelations classAdminRelations = classAdminRelationsService.getOne(classAdminRelationsLambdaQueryWrapper);
            if(classAdminRelations == null){
                return Result.okWithData(new ArrayList<>());
            }
            Integer clazzAdminId = classAdminRelations.getClazzAdminId();

            activitiesLambdaQueryWrapper.eq(Activities::getPublishId, clazzAdminId);

        } else if (level == 2) {
            // 学院
            Integer colId = studentFromDB.getColId();
            LambdaQueryWrapper<CollageAdminRelations> collageAdminRelationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            collageAdminRelationsLambdaQueryWrapper.eq(CollageAdminRelations::getColId, colId);
            CollageAdminRelations adminRelationsServiceOne = collageAdminRelationsService.getOne(collageAdminRelationsLambdaQueryWrapper);
            if(adminRelationsServiceOne == null){
                return Result.okWithData(new ArrayList<>());
            }

            Integer colAdminId = adminRelationsServiceOne.getColAdminId();
            activitiesLambdaQueryWrapper.eq(Activities::getPublishId, colAdminId);

        }

        activitiesLambdaQueryWrapper.eq(Activities::getLevel, level);

        List<Activities> activitiesPageRecords = list(activitiesLambdaQueryWrapper);

        List<ActivityVo> activityVoList = activitiesPageRecords.stream().map(activities -> {
            ActivityVo activityVo = new ActivityVo();
            activityVo.setId(activities.getId());
            activityVo.setTitle(activities.getTitle());
            activityVo.setPic(activities.getPic());
            activityVo.setStartTime(activities.getStartTime());
            activityVo.setEndTime(activities.getEndTime());
            activityVo.setCreateTime(activities.getCreateTime());


            Date startTime = activities.getStartTime();
            Date endTime = activities.getEndTime();
            Date date = new Date();

            // 是否已经结束
            if (date.after(endTime) || date.before(startTime)) {
                activityVo.setStatus(true);
            }

            if (date.after(startTime) && date.before(endTime)) {
                activityVo.setStatus(false);
            }
            if (date.equals(startTime) || date.equals(endTime)) {
                activityVo.setStatus(false);
            }

            return activityVo;
        }).collect(Collectors.toList());


        return Result.okWithData(activityVoList);
    }

    /**
     * 判断活动是否结束
     * @param id 宿舍评优活动参加记录id
     * @return Result
     */
    @Override
    public Result<Boolean> isTimeOut(Integer id) {

        Activities activities = getById(id);

        Date startTime = activities.getStartTime();
        Date endTime = activities.getEndTime();
        Date date = new Date();

        // 是否已经结束
        if (date.after(endTime) || date.before(startTime)) {
            return Result.okWithData(true);
        }

        if (date.after(startTime) && date.before(endTime)) {
            return Result.okWithData(false);
        }
        if (date.equals(startTime) || date.equals(endTime)) {
            return Result.okWithData(false);
        }

        return Result.okWithData(true);
    }

}




